var app = getApp();
var _id = '';
var setTitle = function() {
	wx.setNavigationBarTitle({
		title: '查看详情'
	});
};
var setStatus = function(status) {

	if (status) {
		this.setData({
			baoMin: 'hide',
			baoMinChengGong: ''
		});
	} else {
		this.setData({
			baoMin: '',
			baoMinChengGong: 'hide'
		});
	}
};
Page({
	data: {
		disabled: true
	},
	onLaunch:function(){
		wx.showNavigationBarLoading();
	},
	onShow: function() {
		setTitle();
		
	},
	tel: function(e) {
		var phoneNumber = e.target.dataset.tel;
		
		if (!phoneNumber) {
			return;
		}
		wx.makePhoneCall({
			phoneNumber: phoneNumber
		});
	},

  bindNamechange: function (e) {
    this.data.templ[e.target.dataset.id]['typekey'] = e.detail.value;
    this.setData({ templ: this.data.templ})
  },


  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '填报数据',
      path: '/pages/detail/detail?id=' + _id,
      success: function (res) {
        console.log("转发成功")
      },
      fail: function (res) {
        console.log("转发失败")
      }
    }
  },

  formSubmit: function (e) {
    console.info(e.detail.formId);
    var id = e.target.dataset.id;
    var _this = this;
    var submitObject = e.detail.value || {};
    var status = true;

    for (var key in submitObject) {
      // console.log(key + '=' + submitObject[key]);
      if (!submitObject[key]) {
        for (var e in _this.data.templ){
          var templs =_this.data.templ[e];
          // console.info(templs['required'])
          if (templs['keyname'] == key && templs['switch']){
            wx.showToast({
              title: "填写" + templs['name'],
              icon: 'success',
              image: '/image/message.png',
              duration: 2000
            });
            return;
          }
           
        };
        // status = false;
      }
    }
    // console.log(status);

    if (status) {
      wx.showToast({
        title: '正在提交数据...',
        icon: 'loading',
        duration: 10000
      });
      app.getUserInfo(function (userInfo) {
        submitObject.avatarUrl = userInfo.avatarUrl;
        submitObject.nickName = userInfo.nickName;
        submitObject.openId = userInfo.openId;
        submitObject.pjid=id;
        submitObject.formId = e.detail.formId;
        console.log(submitObject);
        wx.request({
          url: app.globalData.domain + 'api/wxapp_baomin',
          data: submitObject,
          method: 'post',
          success: function (res) {
            _this.setData({
              users: res.data.pjuser,
              number: res.data.pjuser.length
            });
            setStatus.call(_this, true);
            wx.showToast({
              title: '填报成功',
              icon: 'success',
              duration: 2000
            });
          },
          fail: function () { },
          complete: function () { }
        });
      });


    } else {
      wx.showToast({
        title: '数据填写不完整',
        icon: 'success',
        image:'/image/message.png',
        duration: 2000
      });
    }

  },
	onLoad: function(options) {
    _id = options.id;
		var _this = this;
    setStatus.call(_this, false);
    setTitle();
    app.getUserInfo(function (userInfo) {
      wx.request({
        url: app.globalData.domain + 'api/wxapp_get?id=' + options.id,
        data: {
          time: app.globalData.getTime()
        },
        method: 'GET',
        success: function(res) {
          res.data.project.map((e)=>{
            if (e.app_project.id==options.id){
              // console.info(JSON.parse(e.app_project.templ));
              _this.setData(JSON.parse(e.app_project.templ));
              delete e.app_project.templ;
              _this.setData(e.app_project);
              _this.setData({ banner: e.app_tpl.image, number: e.count, name: e.app_user.name });

              if (res && res.data.pjuser && res.data.pjuser.length) {
                //设置用户头像
                _this.setData({
                  users: res.data.pjuser
                });
                //判断当前用户是否填报
                res.data.pjuser.forEach(function (item) {
                  if (item.app_pjuser.openId === userInfo.openId) {
                    _this.data.templ.forEach(function (row) {
                      row['typekey'] = item.app_pjuser[row['keyname']]
                    });
                    _this.setData({ templ: _this.data.templ })
                    // _this.setData(item);
                    setStatus.call(_this, true);
                  }
                });
              }

            }
          })
          wx.hideNavigationBarLoading();
        }
      });
    })



	}
});